package sol.leetcode;

import java.util.Arrays;

/**
 * Search for a Range
 */
public class Issue034 {
    public int[] searchRange(int[] nums, int target) {
        int start = firstNotLessThen(nums, target);
        int end = firstNotLessThen(nums, target + 1) - 1;
        return start > end ? new int[]{-1, -1} : new int[]{start, end};

    }

    //查找第一个不小于target的下标
    private int firstNotLessThen(int[] nums, int target) {
        int start = 0;
        int end = nums.length;
        while (start < end) {
            int m = (start + end) / 2;
            if (nums[m] < target) start = m + 1;
            else end = m;
        }
        return start;
    }

    public static void main(String[] args) {
        int target = -1;
        int[] nums = {1, 2};
        Issue034 sol = new Issue034();
        System.out.println(Arrays.toString(sol.searchRange(nums, target)));
    }
}
